package com.sdry.web.controller.hpf;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sdry.entity.hpf.ProjectInfo;
import com.sdry.service.hpf.ProjectInfoService;
import com.sdry.utils.DataGridResult;
import com.sdry.utils.PageBean;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @ClassName: ProjectInfoController 
 * @Description:项目信息Controller
 * @Author hpf
 * @Date 2018年4月25日 下午3:07:33 
 * @version 1.0
 */
@Controller
@RequestMapping(value="/hpf")
public class ProjectInfoController{

	@Resource
	private ProjectInfoService projectInfoService;
	
	/**
	 * @Title: queryAll
	 * @Description:查询项目信息List
	 * @Author hpf
	 * @Date 2018年4月25日 下午3:20:12 
	 * @version 1.0
	 * @param projectInfo
	 * @return
	 */
	@RequestMapping(value="/queryAll")
	@ResponseBody
	public DataGridResult queryAll(ProjectInfo projectInfo){

		PageBean pageBean = new PageBean();
		Map map = new HashMap();
		map.put("pageSize", pageBean.getPageSize());
		map.put("page", pageBean.getPage());
		if (projectInfo.getPname() != null) {
			map.put("pname", projectInfo.getPname());
		} else {
			map.put("pname", "");
		}
		if (projectInfo.getPnum() != null) {
			map.put("pnum", projectInfo.getPnum());
		} else {
			map.put("pnum", "");
		}
		int count = projectInfoService.selectCount(pageBean);
		List<ProjectInfo> pInfoList = projectInfoService.queryAll(map);

		DataGridResult data = new DataGridResult(count,pInfoList);
		return data;
	}
	
	/**
	 * @Title: addProjectInfo
	 * @Description:新增项目信息
	 * @Author hpf
	 * @Date 2018年4月25日 下午3:59:57 
	 * @version 1.0
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping(value="/addProjectInfo")
	@ResponseBody
	public Map<String,Object> addProjectInfo(@RequestBody Object object) throws ParseException{
		int isSuccess = 0;

		
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String pnum = projectInfoService.projectInfoNumLast();//得到生成的编号
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		simpleDateFormat.format(new Date());
        JSONObject jsonObject = JSONObject.fromObject(object);
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setPnum(pnum);
        projectInfo.setPname((String)jsonObject.get("pname"));
        projectInfo.setPtime((new java.sql.Date(simpleDateFormat.parse((String)jsonObject.get("ptime")).getTime())));
        projectInfo.setPaddress((String)jsonObject.get("paddress"));
        projectInfo.setXstid((String)jsonObject.get("xstid"));
        projectInfo.setSstid((String)jsonObject.get("sstid"));
        projectInfo.setJstid((String)jsonObject.get("jstid"));
        projectInfo.setCstid((String)jsonObject.get("cstid"));
        projectInfo.setCosid((String)jsonObject.get("cosid"));
        projectInfo.setContacts((String)jsonObject.get("contacts"));
        projectInfo.setPcway((String)jsonObject.get("pcway"));
        projectInfo.setPjrtime((new java.sql.Date(simpleDateFormat.parse((String)jsonObject.get("pjrtime")).getTime())));
        projectInfo.setPjetime((new java.sql.Date(simpleDateFormat.parse((String)jsonObject.get("pjetime")).getTime())));
        projectInfo.setItemtype((String)jsonObject.get("itemtype"));
        projectInfo.setFkway((String)jsonObject.get("fkway"));
        projectInfo.setJd((String)jsonObject.get("jd"));
        projectInfo.setPremarks((String)jsonObject.get("premarks"));
        projectInfo.setPman("admin");
        projectInfo.setMoney(Float.parseFloat((String)jsonObject.get("money")));
        projectInfo.setPtimes(new java.sql.Date(System.currentTimeMillis()));
		
		isSuccess = projectInfoService.insert(projectInfo);
		System.out.println(isSuccess);

		Map<String,Object> map = new HashMap<>();
		
		if (isSuccess != 0) {
			map.put("success",true);
		} else {
			map.put("success",false);
		}
		
		return map;
	}
	
	/**
	 * @Title: projectFindByID
	 * @Description:根据pid查询
	 * @Author hpf
	 * @Date 2018年4月27日 下午3:00:06 
	 * @version 1.0
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/projectFindByID")
	@ResponseBody
	public ProjectInfo projectFindByID(long pid){
		ProjectInfo projectInfo = projectInfoService.queryById(pid);
		return projectInfo;
	}
	
	/**
	 * @Title: deleteProject
	 * @Description:批量删除项目信息
	 * @Author hpf
	 * @Date 2018年4月27日 下午3:08:40 
	 * @version 1.0
	 * @param arr
	 * @return
	 */
	@RequestMapping(value="/deleteProject")
	@ResponseBody
	public Map<String,Object> deleteProject(@RequestParam(value = "arr") String arr){
		List<Integer> list = new ArrayList<>();
        list = JSONArray.toList(JSONArray.fromObject(arr));
        int isSuccess = 0;
        for (int i = 0; i < list.size(); i++) {
        	//删除项目信息
            isSuccess = projectInfoService.deleteById(list.get(i).longValue());
		}

		Map<String,Object> map = new HashMap<>();


        if (isSuccess > 0) {
			map.put("success",true);
		} else {
			map.put("success",false);
		}
		return map;
	}
}
